<?php
namespace app\index\controller;
use think\Db;
use app\common\lib;
use think\Request;

class Car extends Base
{
    public function upload()
    {
        if(request()->isAjax()) {
            $data = input('post.');
            $isLogin = lib\Tools::isIndexLogin();
            if(!$isLogin) {
                return show('200','请登录后操作','0');
            }
            $car_info = [
                'user_id' => $this->is_index_login()->id,
                'car_thumb' => $data['car_thumb'],
                'car_number' => $data['car_number'],
                'status' => 1,
            ];
            try{
                $is_has = Model('Car')->where(['car_number'=>$car_info['car_number']])->select();
            }catch (\Exception $e){
                return show('200',$e->getMessage(),'0');
            }
            if($is_has) {
                return show('200','该车辆已经被注册!','0');
            }
            $_res = Model('Car')->save($car_info);
            if(!$_res) {
                return show('200','数据库错误!','0');
            }
            return show('200','上传成功！物业会尽快审核','1');
        }else {
            return $this->fetch();
        }
    }

    public function upload_car()
    {
        if(request()->isAjax()){
            /*判断文件格式、移动文件*/
            $info = lib\UploadFile::UpCarFile();
            if(empty($info)) {
                return show('200','参数为空','0');
            }
            //引入离线SDK
            include EXTEND_PATH.'AipOcr.php';
            // 你的 APPID AK SK
            $APP_ID = '20264625';
            $API_KEY = 'vU7Ufafu6hFuQBIDGg86au7X';
            $SECRET_KEY = 'vSGyFIbHP3Z4vHtzgMOwEQYyPQp7T2So';
            $client = new \AipOcr($APP_ID, $API_KEY, $SECRET_KEY);

            $path = $info['save_name'];
//            $path = lib\UploadFile::thumb_img($path);
            // 如果有可选参数
            $options = array();
            $options["multi_detect"] = "false";
            $image = file_get_contents($path);
            // 调用车牌识别
            $result = $client->licensePlate($image,$options);
            if(isset($result['error_msg'])){
                return show('500','识别不到图中车辆','0');
            }
            $data['car_thumb'] = $path;
            $data['car_number'] = $result['words_result']['number'];
            return show('200','ok',$data);
        }else{
            return $this->redirect('not_found/index/index');
        }
    }

    public function car_list()
    {
        $isLogin = lib\Tools::isIndexLogin();
        if(!$isLogin) {
            return $this->redirect('index/index/index');
        }
        $car_list = Model('Car')->limit_this_msg('5','user_id',$isLogin['id']);
        $this->assign([
            'car_list' => $car_list
        ]);
        return $this->fetch();
    }

    public function del_car()
    {
        if(request()->isGet()){
            $id = request()->param('id');
            try{
                $res = Model('Car')->del_msg($id);
            }catch (\Exception $e){
                return $this->error($e->getMessage());
            }
            if (!$res) {
                return $this->error('数据库错误');
            }
            return $this->redirect('index/car/car_list');
        }else{
            return $this->redirect('index/index/index');
        }
    }
}
